package fnord.jovian.tango;

import fnord.jovian.TheBoard;
import fnord.jovian.tango.TheAssemblyLayout.TheAssemblyOrientation;

public class TheStatusPanel extends ThePanel {
	
	private TheText _currentPlayer, _status;
	private TheButton _passButton, _returnButton;
	private boolean _pass;

	public TheStatusPanel(TheBoard board) {
		super(0,0,300,150,board);
		TheAssemblyLayout layout = new TheAssemblyLayout(TheAssemblyOrientation.VERTICAL,10);
		TheAssemblyLayout horzLayout = new TheAssemblyLayout(TheAssemblyOrientation.HORIZONTAL, 0);
		setTheLayout(layout);
		_currentPlayer = new TheText("", 10, 290, 20, true, true);
		_status = new TheText("", 10, 280, 60, false, true);
		ThePanel buttonPanel = new ThePanel(0,0,280,30,board);
		buttonPanel.setTheLayout(horzLayout);
		_returnButton = new TheButton("Return to Tree", 110, 30, new TheTreeListener(), 10);
		buttonPanel.addComponent(_returnButton);
		_passButton = new TheButton("Pass Turn", 110, 30, new ThePassListener(), 10);
		buttonPanel.addComponent(_passButton);
		buttonPanel.showBackground(false);
		addComponent(_currentPlayer);
		addComponent(_status);
		addComponent(buttonPanel);
	}

	private class TheTreeListener implements TheListener {
		public void doAction() {
			_board.switchTree(0);
		}
	}

	private class ThePassListener implements TheListener {
		public void doAction() {
			if (_pass) {
				_board.pass();
			}
			else {
				_board.cancelAction();
			}
		}
	}
	
	public void setStatus(String status) {
		_status.setText(status);
	}
	
	public void setPlayer(int ID) {
		if (ID == -1) {
			_currentPlayer.setText("Your turn");
			_passButton.setActive(true);
		}
		else {
			_currentPlayer.setText("Player " + ID + "\'s turn");
			_passButton.setText("Pass turn");
			_passButton.setActive(false);
		}
	}
	
	public void pass(boolean pass, String text) {
		_pass = pass;
		if (pass) {
			_passButton.setText("Pass turn");
		}
		else {
			_passButton.setText("Cancel " + text);
		}	
	}
	
}
